﻿<?php
include ('header.php');
if ($admin==1) { 

mysql_query ("CREATE TABLE IF NOT EXISTS `news` (
`id_news` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`login` VARCHAR( 15 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`subject_news` VARCHAR( 50 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`text_news` VARCHAR( 65535 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`hide_news` int(1) NOT NULL  default '0',
`date_news` DATE NOT NULL,
`end_date_news` DATE NOT NULL  default '9999-12-31' 
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;") or die(mysql_error());

if ($act=='add') { ?>	
	<form action="news.php" method="post" name="add">
Заголовок:<br /> <input type="text" name="subject" maxlength="50" size="50" /><br />
Новость:<br />	<textarea name="news" maxlength="65535" rows="15" cols="50"></textarea><br />
Дата&nbsp;окончания<strong>*</strong>:<br /> <input type="text" name="end" maxlength="10" size="10" value="9999-12-31"  id="samplecal_value" /><br />
<div class="scal tinyscal slick" id="samplecal">&nbsp;</div>
    <script type="text/javascript" language="javascript">
    // <![CDATA[
        var options = Object.extend({
            titleformat:'mmmm yyyy',
            closebutton:'<img src="/images/close.gif" />',
            dayheadlength:2,
            prevbutton: '<img src="/images/prev.gif" />',
            nextbutton: '<img src="/images/next.gif" />',
            yearnext: '<img src="/images/nexty.gif" />',
            yearprev: '<img src="/images/prevy.gif" />',
            weekdaystart:1,
            tabular: true,
            planner: false
        });
        var updateyear = function(d){ $('samplecal_value').value = d.format('yyyy-mm-dd'); };
        var samplecal = new scal('samplecal', 'samplecal_value', options);
        // ]]>
    </script>
<input type="checkbox" name="hide" />Скрыть<br />
<input type="hidden" name="act" value="add" />
<input type="submit" value="Добавить новость">
</form>
<?php

}
elseif ($actp=="add") {
	if (!empty($_POST['news']) && !empty($_POST['subject'])) {
echo 'Добавленная новость:';
echo '<br>';
$subject = mysql_real_escape_string($_POST['subject']);
$news = mysql_real_escape_string($_POST['news']);
$end = mysql_real_escape_string($_POST['end']);
echo $subject;
echo '<br>';
echo $news;
echo '<br>';
if (!preg_match("/[0-9]{4}+-[0-9]{2}+-[0-9]{2}/i", $end)) {$colerror=$colerror+1; echo "<div class='error'>Неверно введена дата окончания!</div>";}
if (isset($_POST['hide'])) {$hide='1';} else {$hide='0';};
echo $hide;
mysql_query ("INSERT INTO news VALUES (0  ,'$login','$subject','$news','$hide',NOW(),'$end')") or die(mysql_error());
}
else {echo "Не введена новость!.<br> Пожалуйста, введите новость<br><br>";};

 } 
 elseif ($act=='edit' && !empty($from)) {
	 $newnews = mysql_query ("select * from news WHERE id_news='$from'") or die(mysql_error());
	$rownewnews = mysql_fetch_array($newnews);
	 
	?>
	<form action="news.php" method="post" name="update">
Заголовок:<br /> <input type="text" name="subject" maxlength="50" size="50" value="<?php echo $rownewnews['subject_news']; ?>"/><br />
Новость:<br />	<textarea name="news" maxlength="65535" rows="15" cols="50"><?php echo $rownewnews['text_news']; ?></textarea><br />
<input type="checkbox" name="hide" <?php   if ($rownewnews['hide_news']=='1') {echo 'checked="checked"';} ?>/>Скрыть<br />
<input type="hidden" name="from" value="<?php echo $from; ?>" />
<input type="hidden" name="act" value="update" />
<input type="submit" value="Обновить">
</form>
<?php
	 }
elseif ($actp=="update") {
	if (!empty($_POST['news']) && !empty($_POST['subject'])) {
echo 'Обновленная новость №'.$fromp.' :';
echo '<br>';
$subject = mysql_real_escape_string($_POST['subject']);
$news = mysql_real_escape_string($_POST['news']);
echo $subject;
echo '<br>';
echo $news;
echo '<br>';
if (isset($_POST['hide'])) {$hide='1';} else {$hide='0';};
echo $hide;
mysql_query ("UPDATE `news` SET `subject_news` =  '$subject',
`text_news` = '$news', `hide_news`='$hide'  WHERE `id_news` ='$fromp';") or die(mysql_error());
}
else {echo "Не введена новость!.<br> Пожалуйста, введите новость<br><br>";};

 }
 
 
 
 else {
	 
/* Число строк в таблице*/
$vsego2 = mysql_query("SELECT COUNT(*) FROM news");
list($vsego) = mysql_fetch_row($vsego2);
$nastr=$newspage;

$stranic=(int)($vsego/$nastr);

if ($stranic==1) $stranic++;
if ($stranic==0) $stranic=1;
$str=$_GET['str'];




if ($str=='' || $str==False || $str=='0') $str='1';
$nachvivod=($str-1)*$nastr;
$i=1+$nachvivod;
/* Вывод последней новости. */

/*$my_check = $_POST['selnews'];
$len = sizeof($my_check);
for($i=0; $i<$len; $i++){
 echo $my_check[$i]." - <BR> \n";
};
*/
//Кусок 2:::
$selnews = $_POST['selnews'];
if (!empty($selnews)) {
if ($actp=='delete') {
foreach ($selnews as $news){
 mysql_query ("delete from news where id_news='$news'") or die(mysql_error());
}}
if ($actp=='hide') {
foreach ($selnews as $news){
 mysql_query ("UPDATE news SET `hide_news` =  '1' where id_news='$news'") or die(mysql_error());
}}
if ($actp=='show') {
foreach ($selnews as $news){
 mysql_query ("UPDATE news SET `hide_news` =  '0' where id_news='$news'") or die(mysql_error());
}}}
	
?>
<form action="news.php" method="post" name="enter2">
	


<?php
$newnews = mysql_query ("select * from news order by id_news desc limit $nachvivod,$nastr") or die(mysql_error());
while ($rownewnews = mysql_fetch_array($newnews))
{
	if ($rownewnews['hide_news']=='1') {echo '<small><div class="hide">';}
echo '<input type="checkbox" name="selnews[]" value="',$rownewnews['id_news'],'"><b>',$i,': </b> ';
echo '<a href="news.php?act=edit&from='.$rownewnews['id_news'].'">'.$rownewnews['subject_news'].'</b><br />'.$rownewnews['text_news'].'</a>';
	if ($rownewnews['hide_news']=='1') {echo '</div></small>';}
echo '<div id=date align=right>',$rownewnews['date_news'],'</div>';
echo '<br>';
$i++;}
?>
<select size="1" name="act">
  <option value="hide">Скрыть</option>
  <option value="show">Показать</option>
  <option value="delete">Удалить</option>
</select>
<input type="submit" value="Далее"  />
</form>

<?php
echo 'Cтраницы:<br>';
for ($vs = 1; $vs <= $stranic; $vs++) {
if ($vs==$str)
{
	echo '&nbsp;';
	//echo '&nbsp;&nbsp;&nbsp;';	//if ($vs!=0 && $vs!=1) {echo '<a href=/index.php?page=news&str=',$vs-1,'><small>',пред,'</small></a>&nbsp;';};
	echo '<a href=news.php?str=',$vs,'><big><b>',$vs,'</b></big></a>&nbsp;';
	//if ($vs!=$stranic) {echo '<a href=/index.php?page=news&str=',$vs+1,'><small>', след ,'</small></a>&nbsp;';}
	//echo '&nbsp;&nbsp;&nbsp;';
	echo '&nbsp;';
} else {echo '<a href=news.php?str=',$vs,'>',$vs,'</a>&nbsp;';};}
}
include ('footer.php');
}
?>